/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package root;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JPanel;

/**
 *
 * @author Bernhard
 */
public class grafikPanel extends JPanel {
    
    private Koordinatensystem mg;

    public grafikPanel(){
        super();
        this.setBackground(Color.WHITE);
    }

    /**
     * @param g GraphicsObject on which to work
     */
    @Override
    public void paintComponent(Graphics g){
        super.paintComponent(g);
        scale();
        g.setFont(new Font("SansSerif", Font.PLAIN, 9));
        double stepWidth = 10*Controler.getScale();
        System.out.println("Step Width: "+stepWidth+" after cast: " + (int)stepWidth);
        mg = new Koordinatensystem(g, 1/getWidth() + 10, getHeight() - 10, getWidth(), getHeight(), 1, 2.5, (int)stepWidth, (int)stepWidth);
        mg.drawAxes(false);
        mg.drawRaster();

        g.setFont(new Font("SansSerif", Font.PLAIN, 12));
        g.setColor(Color.red);
        updateFromList();
    }

    public void updateFromList(){
        for (int i = 0; i < Controler.getInstance().getObjectsList().size(); i++){
            Graphics2D g2d = (Graphics2D) super.getGraphics();
            g2d.scale(Controler.getScale(), Controler.getScale());
            Controler.getInstance().getObjectsList().get(i).draw(g2d, Color.BLACK);
        }
    }
    
    private void scale(){
        double scaleX = (double)this.getWidth()/(double)Controler.getInstance().maxX();
        double scaleY = (double)this.getHeight()/(double)Controler.getInstance().maxY();
        if(scaleX < scaleY) Controler.setScale(scaleX-0.1);
        else Controler.setScale(scaleY-0.1);
    }

    public void clear(){
        this.revalidate();
        this.paint(super.getGraphics());
    }
    
    public void dangerClear(){
        this.revalidate();
        this.repaint();
    }
}
